﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Util;
using FastExecutor.ORG.Entity;
using FastExecutor.ORG.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastExecutor.ORG.DB
{
    public class DB_OU : FastTemplateDB<Frame_OU>
    {

        public override void Insert(Frame_OU Entity)
        {
            Entity.RowGuid = CommonUtil.CreateCommonGuid();
            Entity.AddDate = DateTime.Now;
            Entity.OUCode = DataBuilder.CreateTableFieldCodeValue("Frame_OU", "RowGuid", "OUCode", "ParentOUGuid", Entity.ParentOUGuid);
            base.Insert(Entity);
        }

        public override void Update(Frame_OU Entity)
        {
            Frame_OU DBEntity = FindOne(Entity.RowGuid);
            if (DBEntity.ParentOUGuid != Entity.ParentOUGuid)
            {
                Entity.OUCode = DataBuilder.CreateTableFieldCodeValue("Frame_OU", "RowGuid", "OUCode", "ParentOUGuid", Entity.ParentOUGuid);
            }
            base.Update(Entity);
        }

        public override void Update(Newtonsoft.Json.Linq.JObject JsonEntity)
        {
            Frame_OU DBEntity = FindOne(Convert.ToString(JsonEntity.GetValue("RowGuid")));
            if (DBEntity.ParentOUGuid != Convert.ToString(JsonEntity.GetValue("ParentOUGuid")))
            {
                JsonEntity["OUCode"] = DataBuilder.CreateTableFieldCodeValue("Frame_OU", "RowGuid", "OUCode", "ParentOUGuid", Convert.ToString(JsonEntity.GetValue("ParentOUGuid")));
            }
            base.Update(JsonEntity);
        }

        public override void BatchUpdate(List<Frame_OU> EntityList)
        {
            foreach (Frame_OU Entity in EntityList)
            {
                Frame_OU DBEntity = FindOne(Entity.RowGuid);
                if (DBEntity.ParentOUGuid != Entity.ParentOUGuid)
                {
                    Entity.OUCode = DataBuilder.CreateTableFieldCodeValue("Frame_OU", "RowGuid", "OUCode", "ParentOUGuid", Entity.ParentOUGuid);
                }
            }
            base.BatchUpdate(EntityList);
        }

        public override void BatchUpdate(Newtonsoft.Json.Linq.JArray JsonArray)
        {
            foreach (JObject JsonEntity in JsonArray)
            {
                Frame_OU DBEntity = FindOne(Convert.ToString(JsonEntity.GetValue("RowGuid")));
                if (DBEntity.ParentOUGuid != Convert.ToString(JsonEntity.GetValue("ParentOUGuid")))
                {
                    JsonEntity["OUCode"] = DataBuilder.CreateTableFieldCodeValue("Frame_OU", "RowGuid", "OUCode", "ParentOUGuid", Convert.ToString(JsonEntity.GetValue("ParentOUGuid")));
                }
            }
            base.BatchUpdate(JsonArray);
        }

        public override void BatchDelete(List<string> KeyGuidList)
        {
            foreach (string KeyGuid in KeyGuidList)
            {
                @DBUtil.ExecuteSQL(string.Format("delete from Frame_ModuleRight where RightGuid='{0}'", KeyGuid));
            }
            base.BatchDelete(KeyGuidList);
        }


        public string GetFullPathOUName(string OUGuid)
        {
            string OUName = string.Empty;
            List<Frame_OU> AllOUList = FindAll();
            CaculateSubOuName(AllOUList, OUGuid, ref OUName);
            return OUName;
        }

        private void CaculateSubOuName(List<Frame_OU> AllOUList, string OUGuid, ref string OUName)
        {
            List<Frame_OU> AllOUListFilter = AllOUList.Where(a => a.RowGuid == OUGuid).ToList();
            if (AllOUListFilter.Count > 0)
            {
                Frame_OU OU = AllOUListFilter[0];
                if (!string.IsNullOrEmpty(OUName))
                {
                    OUName = OU.OUName + " — " + OUName;
                }
                else
                {
                    OUName = OU.OUName;
                }

                CaculateSubOuName(AllOUList, OU.ParentOUGuid, ref OUName);
            }
        }

    }
}
